package request

import (
	"fmt"
	"github.com/guonaihong/gout"
	"github.com/tidwall/gjson"
	"main/model"
	"main/pipline"
	"main/utils"
)

// 话题榜 1
func WeHt(types string) (msg string, IsSuccess bool) {
	fmt.Println(types)
	// 使用defer, 表示函数完成时将等待组值减1
	fmt.Println(`=======微博话题榜启动中======`)
	text, code := "", 0
	RequestUrl := fmt.Sprintf(`https://weibo.com/ajax/statuses/topic_band?sid=v_weibopro&category=all&page=1&count=%d`, 10)
	gout.GET(RequestUrl).BindBody(&text).Code(&code).Do()
	for _, node := range gjson.Get(text, `data.statuses`).Array() {
		fmt.Println(`微博话题榜`)
		// 生成 hashId
		hashId := utils.Base64Encrypt("WeHt" + "_" + node.Get(`mid`).String())
		err := pipline.SavePost(&model.PostInfo{
			RefId:       hashId,
			Title:       node.Get(`topic`).String(),
			HtmlContext: node.Get(`mblog.text`).String(),
			Status:      0,
			WebType:     2, //1 热榜 2 话题榜
			CreateTime:  utils.TimeNow(),
			UpdateTime:  utils.TimeNow(),
			CreateOn:    utils.TimeNow(),
		})
		fmt.Println(`热榜标题：`, node.Get(`topic`).String())
		fmt.Println(`热榜内容总结：`, node.Get(`summary`).String())
		fmt.Println(`热榜详情内容：`, node.Get(`mblog.text`).String())
		fmt.Println(`++++++++++++++++++++++`)
		if err != nil {
			return "微博话题榜抓取失败！", false
		}
	}
	return "微博话题榜抓取完成", true
}

//微博热搜榜 2
func WeRS(types string) (msg string, IsSuccess bool) {
	fmt.Println(types)
	fmt.Println(`=======微博热榜榜启动中======`)
	text, code := "", 0
	//https://weibo.com/ajax/statuses/hot_band
	RequestUrl := fmt.Sprintf(`https://weibo.com/ajax/statuses/hot_band`)
	gout.GET(RequestUrl).BindBody(&text).Code(&code).Do()
	for _, node := range gjson.Get(text, `data.band_list`).Array() {
		fmt.Println(`微博话题榜`)
		hashId := utils.Base64Encrypt("WeHt" + "_" + node.Get(`mid`).String())
		err := pipline.SavePost(&model.PostInfo{
			RefId:       hashId,
			Title:       node.Get(`note`).String(),
			HtmlContext: node.Get(`mblog.text`).String(),
			Status:      0,
			//RefName: node.Get(`summary`).String(),
			WebType:    1,
			CreateTime: utils.TimeNow(),
			UpdateTime: utils.TimeNow(),
			CreateOn:   utils.TimeNow(),
		})
		if err != nil {
			return "微博热榜榜抓取失败", false
		}
	}
	fmt.Print(``)
	return "微博热榜榜抓取完成", true
}
